<template>
  <div class="base-container" id="main">
    <t-space direction="vertical">
      <t-form labelAlign="top" ref="form" :data="formData" :rules="rules" class="step-form table-form"
              labelWidth="140px">
        <t-row>
          <t-col :span="2" class="title"><span class="require">*</span>年度</t-col>
          <t-col :span="2">
            <t-form-item label="年度" name="year">
              <t-select clearable v-model="formData.year" :options="yearOption" />
            </t-form-item>
          </t-col>
        </t-row>
      </t-form>
      <t-row>
        <t-col :span="1"></t-col>
        <t-col :span="10">
          <t-table
            rowKey="index"
            :data="data"
            :columns="columns"
            stripe
            size="small"
            hover
            tableLayout="fixed"
            bordered
            cellEmptyContent="-"
          >
            <template #name="{row}">
              <p :class="row.class">{{ row.name }}</p>
            </template>
            <template #num="{row}">
              <t-input-number min="0" :max="selectMax(row)" :tips="getTips(row)" size="large" v-model="formData[`${row.num}`]" ></t-input-number>
            </template>
            <template #footerSummary>
              <div class="t-table__row-filter-inner">说明：主要平衡关系
                KR110≤KR100，KR120≤KR100，KR130≤KR100，KR140≤KR100，KR150≤KR100，KR160≤KR100。
                KR210≤KR200，KR220≤KR200，KR230≤KR200，KR240≤KR200。
              </div>
            </template>
          </t-table>
        </t-col>
        <t-col :span="1"></t-col>
      </t-row>
      <t-row style="margin-top: 20px;text-align: center">
        <t-col :span="12">
          <t-space :size="10">
            <t-button v-if="!isUpdate" :disabled="disabled" theme="primary" :loading="applyLoading" @click="onSubmit(1)">{{ applyLoading ? '提交中...' : '暂存' }}
            </t-button>
            <t-button v-if="!isUpdate" :disabled="disabled" theme="primary" :loading="applyLoading" @click="onSubmit(2)">{{ applyLoading ? '提交中...' : '提交' }}
            </t-button>
            <t-button v-if="isUpdate" :disabled="disabled" theme="default" :loading="applyLoading" @click="onSubmit(3)">{{ '取消' }}
            </t-button>
            <t-button v-if="isUpdate" :disabled="disabled" theme="primary" :loading="applyLoading" @click="onSubmit(4)">{{ applyLoading ? '修改中...' : '确认' }}
            </t-button>
          </t-space>
        </t-col>
      </t-row>
    </t-space>
  </div>
</template>

<script>
import { VITE_FILE_DOMAIN } from '@/api/upload'
export default {
  name: "personnel-add-edit",
  dicts: [],
  components: {
  },
  data() {
    return {
      yearOption: [],
      file_domain: VITE_FILE_DOMAIN,
      deptMenuOptions: [],
      columns: [
        // { colKey: 'index', title: '序号', width: '100' },
        { colKey: 'name', title: '指标名称', width: '500', },
        { colKey: 'code', title: '代码', align: "center" },
        { colKey: 'jldw', title: '计量单位', align: "center"},
        { colKey: 'num', title: '数量', align: "center"},
      ],
      rules: {
        projectName: [
          {
            required: true,
            message: "项目名称不能为空",
          }
        ],
      },
      data: [],
      preData: {
        name: [
          '一、科普专职人员',
          '   其中：中级职称及以上或本科及以上学历人员',
          '       女性',
          '       农村科普人员',
          '       管理人员',
          '       科普创作（研发）人员',
          '       科普讲解（辅导）人员',
          '二、科普兼职人员',
          '    其中：中级职称及以上或本科及以上学历人员',
          '        女性',
          '        农村科普人员',
          '        科普讲解（辅导）人员',
          '        当年实际投入工作量',
          '三、注册科普（技）志愿者'
        ],
        code:[
          '人',
          '人',
          '人',
          '人',
          '人',
          '人',
          '人',
          '人',
          '人',
          '人',
          '人',
          '人',
          '人天',
          '人',
        ],
        jldw: [
          'KR100',
          'KR110',
          'KR120',
          'KR130',
          'KR140',
          'KR150',
          'KR160',
          'KR200',
          'KR210',
          'KR220',
          'KR230',
          'KR240',
          'KR250',
          'KR300',
        ],
        num: [
          'zzry',
          'zzzjzcysry',
          'zznxry',
          'zznckpry',
          'zzglry',
          'zzkpczry',
          'zzkpjjry',
          'jzry',
          'jzzjzcysry',
          'jznxry',
          'jznckpry',
          'jzkpjjry',
          'sjtrgzl',
          'zckjzyz'
        ],
        class: [
          '',
          'indent2',
          'indent4',
          'indent4',
          'indent4',
          'indent4',
          'indent4',
          '',
          'indent2',
          'indent4',
          'indent4',
          'indent4',
          'indent2',
          '',
        ],
        inputDisabled: []
      },
      applyLoading: false,
      formData: {
        localId: '',
        year: new Date().getFullYear(),
        zzry: 0,
        zzzjzcysry: 0,
        zznxry: 0,
        zznckpry: 0,
        zzglry: 0,
        zzkpczry: 0,
        zzkpjjry: 0,
        jzry: 0,
        jzzjzcysry: 0,
        jznxry: 0,
        jznckpry: 0,
        jzkpjjry: 0,
        sjtrgzl: 0,
        zckjzyz: 0,
      },
      isUpdate: false,
      arr1 : ['zzzjzcysry', 'zznxry', 'zznckpry', 'zzglry', 'zzkpczry', 'zzkpjjry'],
      arr2 : ['jzzjzcysry', 'jznxry', 'jznckpry', 'jzkpjjry']
    }
  },
  computed: {
    disabled() {
      const a = this.formData
      for (const val of this.arr1) {
        if (a[val] > a.zzry) return true
      }
      for (const val of this.arr2) {
        if (a[val] > a.jzry) return true
      }
      return false
    }
  },
  created() {
    for (let i = 2030; i >= 2000; i--) {
      this.yearOption.push({key: i, label: i.toString(), value: i.toString()})
    }
    const taskId = this.$route.params && this.$route.params.taskId;
    this.isUpdate = this.$route.query.isUpdate === 'true'
    Object.assign(this.formData, this.$route.query.currentRow)
    if (!taskId) {
      this.$message.error("缺少id");
      this.close();
      return false;
    }
    this.formData.localId = taskId

    for (let i = 0; i < this.preData.name.length; i++) {
      this.data.push({
        index: i + 1,
        name: this.preData.name[i],
        code: this.preData.code[i],
        jldw: this.preData.jldw[i],
        num: this.preData.num[i],
        class: this.preData.class[i],
      })
    }
  },

  methods: {
    getTips(row) {
      let parent = null
      let jldw = null
      if (this.arr1.includes(row.num)) {
        parent = this.formData.zzry
        jldw = 'KR100'
      } else if (this.arr2.includes(row.num)) {
        parent = this.formData.jzry
        jldw = 'KR200'
      } else {
        return undefined
      }
      if (this.formData[row.num] <= parent) {
        return undefined
      }
      return `${row.jldw}<=${jldw}`
    },
    selectMax(row) {
      if (this.arr1.includes(row.num)) {
        return this.formData.zzry
      }
      if (this.arr2.includes(row.num)) {
        return this.formData.jzry
      }
      return 'Infinity'
    },
    onSubmit(type) {
      if (!this.formData.year) {
        this.$message.error('请选择年份！');
        return
      }
      if (type === 1) {
        this.$api.kpzh.personnel.add(this.formData).then(res => {
          this.$message.success('暂存成功！');
          this.$tab.closeOpenPage({path: this.$route.meta.activeMenu || "/kpzh/personnel/personnel-my"});
        })
      } else if (type === 2) {
        this.$api.kpzh.personnel.submit(this.formData).then(res => {
          this.$message.success('提交成功！');
          this.$tab.closeOpenPage({path: this.$route.meta.activeMenu || "/kpzh/personnel/personnel-my"});
        })
      } else if (type === 3) {
        this.$tab.closeOpenPage({path: this.$route.meta.activeMenu || "/kpzh/personnel/personnel-my"});
      } else if (type === 4) {
        this.$api.kpzh.personnel.update(this.formData).then(res => {
          this.$message.success('修改成功！');
          this.$tab.closeOpenPage({path: this.$route.meta.activeMenu || "/kpzh/personnel/personnel-my"});
        })
      } else {
        this.$message.error('type错误！');
      }
    },
    close() {
      this.$tab.closeOpenPage({path: "/kpzh/personnel/personnel-my"});
    }
  }
}
</script>

<style lang="less" scoped>
/deep/ .t-table__empty {
  min-height: initial;
}

</style>
